package com.cnc.servlet;

import com.cnc.pojo.UserCourse;
import com.cnc.service.CourseService;
import com.cnc.service.Impl.CourseServiceImpl;
import com.cnc.service.Impl.UserCourseServiceImpl;
import com.cnc.service.UserCourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/userCourse")
public class UserCourseServlet extends BaseServlet{
    private static UserCourseService ucs=new UserCourseServiceImpl();

    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");
        PrintWriter writer = resp.getWriter();
        writer.print(ucs.getSelectList(currentPage,pageSize,search));
    }

    protected void delSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if(data!=null){
            resp.getWriter().print(ucs.delAllSelect(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String id = req.getParameter("id");
        resp.getWriter().print(ucs.update(id,cid));
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void haveBuy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String uid = req.getParameter("uid");
        resp.getWriter().print(ucs.haveBuy(uid,cid));
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String uid = req.getParameter("uid");
        ucs.add(uid,cid);
        resp.sendRedirect("http://127.0.0.1:5500/UStudy/pages/videoDetail.html?"+cid);
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }
    protected void getMyCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        if(uid!=null){
            resp.getWriter().print(ucs.getMyCourse(uid));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }
}
